package com.org.khtn.command;

public class QueryPattern {
	public static String SELECT_PATTERN;
	public static String INSERT_PATTERN;
	public static String DELETE_PATTERN;
	public static String UPDATE_PATTERN;
	public static String OBJECT = "(\\D\\w*)(\\.\\D\\w*)?";

	private QueryPattern() {

	}

	public static void createPattern() {
		new QueryPattern();
		createDeletePattern();
		createSelectPattern();
		createInsertPattern();
		createUpdatePattern();
	}

	private static void createInsertPattern() {
		// TODO Auto-generated method stub
		String object = "((\\d+)|('.*'))";
		String ob = object + "(\\s*,\\s*" + object + ")*";
		String object1 = "(\\D\\w*)(\\.\\D\\w*)?";

		INSERT_PATTERN = "\\s*insert\\s+into\\s+" + object1 + "\\s+\\(\\s*"
				+ object1 + "(\\s*,\\s*" + object1 + ")*\\)\\s+values\\s+\\("
				+ "\\s*" + ob + "\\s*\\)\\s*";

	}

	private static void createDeletePattern() {
		String object = "(\\D\\w*)(\\.\\D\\w*)?";
		String wherePattern1 = "\\s*\\S+\\s*(>=|<=|=|<|>)\\s*\\S+\\s*";
		String wherePattern2 = "(" + wherePattern1 + "\\s+(and|or)\\s+)+"
				+ wherePattern1;
		String wherePattern = "\\s+where\\s+((" + wherePattern1 + ")|("
				+ wherePattern2 + "))\\s*";

		String fromPattern = "\\s*delete\\s+(from\\s+)?" + object + "\\s*";
		String delete_1 = fromPattern + wherePattern ;
		String delete_2 = fromPattern;
		DELETE_PATTERN = delete_1 + "|" + delete_2;

	}

	private static void createSelectPattern() {
		String object = "(\\D\\w*)(\\.\\D\\w*)?\\s*";

		String tableOrField = "(" + object + ")|((" + object + "),\\s*)*"
				+ object;
		String wherePattern1 = "\\s*\\S+\\s*(>=|<=|=|<|>)\\s*\\S+\\s*";
		String wherePattern2 = "(" + wherePattern1 + "\\s+(and|or)\\s+)+"
				+ wherePattern1;
		String wherePattern = "\\s+where\\s+((" + wherePattern1 + ")|("
				+ wherePattern2 + "))\\s*";

		String fromPattern = "(\\s+from\\s+(" + tableOrField + "))\\s*";
		String joinPattern = "(\\s+from\\s+" + object + "\\s+join\\s+" + object
				+ "\\s+on\\s+" + wherePattern1 + ")\\s*";
		String selectPattern = "\\s*+(?i)select\\s+(\\*|" + tableOrField + "|("
				+ tableOrField + "\\.\\*" + "))\\s*";
		String queryPattern1 = "(" + selectPattern + fromPattern + ")";
		String queryPattern2 = "(" + queryPattern1 + wherePattern + ")";
		String queryPattern3 = "(" + selectPattern + joinPattern + ")";
		String queryPattern3_1 = "(" + queryPattern3 + "(" + wherePattern
				+ ")?)";
		String pattern1 = "((exists)|(" + QueryPattern.OBJECT + "\\s+(?i)in))";

		String wherePattern5 = "\\s+where\\s+\\(" + pattern1 + "\\s+\\(\\s*"
				+ queryPattern2 + "\\s*\\)\\s*\\)";

		String queryPattern7 = "(" + queryPattern1 + wherePattern5 + ")";
		String queryPattern4 = "(" + selectPattern + "\\s+from\\s\\(\\s*" + "("
				+ queryPattern1 + "|" + queryPattern2
				+ "\\s*)\\)\\s+as\\s+\\w+)\\s*";
		String queryPattern5 = "(" + queryPattern4 + "(" + wherePattern + ")?)";
		String having = "\\s+having\\s+" + wherePattern + "\\s*";
		// String groupby1 = "\\s+group\\s+by\\s+" + object ;
		// String groupby = "?(" + groupby1 + "\\s*)";
		String groupby = "(\\s+(?i)group\\s+(?i)by\\s+" + object + ")";
		String orderby = "(\\s*order\\s+by\\s+" + ")?\\s*";
		// String groupby = "(" + groupby1 + "(" + having + ")?\\s*)?\\s*";

		String ssPattern2 = "(" + queryPattern7 + "|" + queryPattern5 + "|"
				+ queryPattern3_1 + "|" + queryPattern2 + "|" + queryPattern1
				+ ")\\s*";
		String ssPattern1 = ssPattern2 + groupby + "\\s*";
		String ssPattern = "(" + ssPattern1 + ")|(" + ssPattern2 + ")";
		SELECT_PATTERN = ssPattern;

	}

	private static void createUpdatePattern() {
		String table = "(\\D\\w*)(\\.\\D\\w*)?";

		String object = "((\\d+)|('.*')|(" + table + "))";
		String wherePattern1 = "\\s*\\S+\\s*(>=|<=|=|<|>)\\s*\\S+\\s*";
		String wherePattern2 = "(" + wherePattern1 + "\\s+(and|or)\\s+)+"
				+ wherePattern1;
		String wherePattern = "\\s+where\\s+((" + wherePattern1 + ")|("
				+ wherePattern2 + "))\\s*";
		String set1 = "(" + object + "\\s*=\\s*" + object + ")\\s*";

		String secondPattern = "\\s+set\\s+" + set1 + "(\\s*,\\s*" + set1
				+ ")*\\s*";
		String firstPattern = "\\s*update\\s+" + object + "\\s*";
		UPDATE_PATTERN = firstPattern + secondPattern + wherePattern;
	}
}
